package com.sangmo.fts.api.factoring;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.boot.framework.data.filter.PageResult;
import com.sangmo.boot.framework.data.filter.ValueFilter;
import com.sangmo.boot.framework.data.json.ExtViewHelper;
import com.sangmo.boot.framework.plugin.dataview.data.DataViewPageSearch;
import com.sangmo.fts.process.factoring.interfaces.FfTransferApplyBankConfirmRequest;
import com.sangmo.fts.process.factoring.interfaces.FfTransferApplyCreation;
import com.sangmo.fts.process.factoring.model.FfTransferApply;
import com.sangmo.fts.process.factoring.service.FfTransferApplyService;
import com.sangmo.fts.process.receivable.interfaces.RpfPledgeContractCreation;
import com.sangmo.fts.restructure.meta.TrainingApi;
import com.sangmo.fts.restructure.meta.TrainingProcess;
import com.sangmo.fts.training.model.TrainingHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashMap;

@TrainingApi
@Controller
@RequestMapping("/ffTransferApply/v1")
@Access("ffTransferApply")
public class FfTransferApplyApi {

    @Autowired
    private FfTransferApplyService ffTransferApplyService;

    @Access(":create")
    @PostMapping(value = "/{trainingId}/create")
    public @ResponseBody
    ApiResponse<FfTransferApply> create(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid FfTransferApplyCreation obj) {
        obj.setTrainingId(trainingId);
        return ApiResponse.make(ffTransferApplyService.create(obj));
    }

    @Access(":remove")
    @PostMapping("/{trainingId}/{id}/remove")
    public @ResponseBody ApiResponse remove(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffTransferApplyService.deleteById(id);
        return ApiResponse.make();
    }

    @Access(":read")
    @PostMapping("/{trainingId}/search")
    public @ResponseBody ApiResponse<PageResult<FfTransferApply>> search(@PathVariable("trainingId") Long trainingId, @RequestBody @Valid DataViewPageSearch<HashMap> ps) {
        ExtViewHelper.setGroup("FfTransferApply.search", 1);
        ps.getFilters().add(new ValueFilter("trainingId", ValueFilter.OP_EQ, trainingId));
        return ApiResponse.make(ffTransferApplyService.search(ps));
    }

    @Access(":read")
    @PostMapping("/{trainingId}/{id}/read")
    public @ResponseBody ApiResponse<FfTransferApply> read(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ExtViewHelper.setGroup("FfTransferApply.detail", 1);
        return ApiResponse.make(ffTransferApplyService.find(id));
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_TRANSFER_APPLY_SELLER_SUBMIT_9)
    @Access(":submit")
    @PostMapping("/{trainingId}/{id}/submit")
    public @ResponseBody ApiResponse submit(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffTransferApplyService.submit(id);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_TRANSFER_APPLY_BANK_CONFIRM_10)
    @Access(":bankConfirm")
    @PostMapping({"/{trainingId}/{id}/bank/confirm"})
    @ResponseBody
    public ApiResponse bankConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid FfTransferApplyBankConfirmRequest request) {
        ffTransferApplyService.bankConfirm(id, request);
        return ApiResponse.make();
    }

    @TrainingProcess(node = TrainingHistory.FfProcessNodes.FF_TRANSFER_APPLY_BUYER_CONFIRM_11)
    @Access(":buyerConfirm")
    @PostMapping({"/{trainingId}/{id}/buyer/confirm"})
    @ResponseBody
    public ApiResponse buyerConfirm(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id) {
        ffTransferApplyService.buyerConfirm(id);
        return ApiResponse.make();
    }


}
